package cn.lili.modules.payment.serviceimpl;

import cn.hutool.json.JSONObject;
import cn.lili.common.utils.CurrencyUtil;
import cn.lili.common.utils.SnowFlake;
import cn.lili.modules.payment.entity.dos.WechatApplyment;
import cn.lili.modules.payment.entity.dos.WithdrawLog;
import cn.lili.modules.payment.entity.dto.WithdrawLogSearchParams;
import cn.lili.modules.payment.entity.enums.ApplymentStatusEnum;
import cn.lili.modules.payment.mapper.WithdrawLogMapper;
import cn.lili.modules.payment.service.WechatApplymentService;
import cn.lili.modules.payment.service.WithdrawLogService;
import cn.lili.modules.payment.util.EcommerceUtil;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分账记录 业务实现
 *
 * @author Chopper
 * @since 2020-12-19 09:25
 */

@Slf4j
@Service
@RequiredArgsConstructor
public class WithdrawLogServiceImpl extends ServiceImpl<WithdrawLogMapper, WithdrawLog> implements WithdrawLogService {

    @Autowired
    private EcommerceUtil ecommerceUtil;
    @Autowired
    private WechatApplymentService wechatApplymentService;

    @Override
    public IPage<WithdrawLog> withdrawLogPage(WithdrawLogSearchParams withdrawLogSearchParams) {
        return this.page(PageUtil.initPage(withdrawLogSearchParams), withdrawLogSearchParams.queryWrapper());
    }

    @Override
    public WithdrawLog getWithdrawLog(String id) {
        return this.getById(id);
    }

    @Override
    public void subWithdraw(String subMchid) {
        //查询账户余额
        JSONObject jsonObject = ecommerceUtil.subNowBalance(subMchid);
        Integer availableAmount = jsonObject.getInt("available_amount");

        //用户金额大于0分则可以调用提现
        if (availableAmount > 0) {
            String outOrderNo = SnowFlake.createStr("SW");
            //发起提现
            jsonObject = ecommerceUtil.subWithdraw(subMchid, outOrderNo, availableAmount);

            WithdrawLog withdrawLog = new WithdrawLog();
            withdrawLog.setSubMchid(subMchid);
            withdrawLog.setOutRequestNo(outOrderNo);
            withdrawLog.setAmount(CurrencyUtil.reversalFen(availableAmount));
            withdrawLog.setWithdrawId(jsonObject.getStr("withdraw_id"));
            withdrawLog.setStatus("INIT");

            //保存提现记录
            this.save(withdrawLog);
        }

    }

    @Override
    public void subWithdraw() {
        List<WechatApplyment> wechatApplymentList = wechatApplymentService.list(new LambdaQueryWrapper<WechatApplyment>()
                .eq(WechatApplyment::getStatus, ApplymentStatusEnum.APPLYMENT_STATE_FINISHED.name()));

        //查询所有的二级商户，进行提现
        for (WechatApplyment wechatApplyment : wechatApplymentList) {
            this.subWithdraw(wechatApplyment.getSubMchid());
        }
    }

    @Override
    public void querySubWithdraw() {
        List<WithdrawLog> withdrawLogList = list(new LambdaQueryWrapper<WithdrawLog>()
                .eq(WithdrawLog::getStatus, "INIT")
                .or()
                .eq(WithdrawLog::getStatus, "CREATE_SUCCESS"));
        for (WithdrawLog withdrawLog : withdrawLogList) {
            //查询提现状态
            JSONObject jsonObject = ecommerceUtil.querySubWithdrawByOutRequestNo(withdrawLog.getSubMchid(), withdrawLog.getOutRequestNo());
            this.update(new LambdaUpdateWrapper<WithdrawLog>()
                    .eq(WithdrawLog::getOutRequestNo, withdrawLog.getOutRequestNo())
                    .set(WithdrawLog::getStatus, jsonObject.getStr("status")));
        }

    }


}
